Page({
  data:{
    num:0,
    sumPrice:0,
    sumNum:0,
    foodlist:[],
    cartList:[],
    store:false
  },
  onLoad()
  {
    let message = wx.getStorageSync('message')
    let user = wx.getStorageSync('user')
    if(user.length == 0)
    {
      wx.showToast({
        icon:'none',
        title: '请先登陆',
        duration:2000,
        success:function()
        {
          // setTimeout(function() 
          // {
            wx.switchTab({
              url: '/pages/person/person',
            })
          // }, 2000);
        }
      })
    }
    else
    {
      if(message.length == 0)
      {
        wx.showToast({
          icon:'none',
          title: '您还没有填写地址',
          duration:2000,
          success:function()
          {
            setTimeout(function() 
            {
              wx.navigateTo({
                url: '/pages/address/address',
              })
            }, 2000);
          }
        })
      }
      else
      {
        let name = message[0]
        let phone = message[1]
        let address = message[2]
        this.setData({
          name:name,
          phone:phone,
          address:address
        })
      }
    }
    let myCart = wx.getStorageSync('mycart')
    if(myCart.length > 0)
    {
      console.log(myCart)
      let showprice = 0,showsum = 0
      myCart.forEach(item=>{
        showprice += item.price*item.num
        showsum += item.num
        this.setData({
          sumPrice:showprice,
          sumNum:showsum,
          cartList:myCart,
          listid:myCart,
        })
      })
    }
  },
  onShow()
  {
    this.onLoad()
  },
  add(e)
  {
    let myCart = wx.getStorageSync('mycart')
    let id = e.currentTarget.dataset.id
    myCart.forEach(item=>{
      if(item._id == id)
      {
        item.num += 1
      }
    })
    wx.setStorageSync('mycart', myCart)
    myCart = wx.getStorageSync('mycart')

    myCart.forEach(item=>{
      if(item._id == id)
      {
        this.setData({
          sumPrice:this.data.sumPrice += item.price,
          sumNum:this.data.sumNum += 1,
          cartList:myCart,
          listid:myCart
        })
      }
    })
    console.log(myCart)
  },
  del(e)
  {
    let myCart = wx.getStorageSync('mycart')
    let id = e.currentTarget.dataset.id

    for(let i=0;i<myCart.length;i++)
    {
      if(myCart[i]._id == id)
      {
        myCart[i].num -= 1
        if(myCart[i].num == 0)
        {
          console.log(myCart[i].name+'空了')
        }
      }
    }
    wx.setStorageSync('mycart', myCart)
    myCart = wx.getStorageSync('mycart')

    let sumStore = 0
    myCart.forEach(item=>{
      if(item._id == id)
      {
        this.data.sumPrice -= item.price
        var midSumPrice = this.data.sumPrice
        midSumPrice = Math.floor(midSumPrice * 100) / 100 
        if(midSumPrice < 0)
        {
          midSumPrice = 0
        }
        this.setData({
          sumPrice:midSumPrice,
          sumNum:this.data.sumNum -= 1,
          cartList:myCart,
          listid:myCart
        })
      }
      sumStore += item.num
    })
    if(sumStore == 0)
    {
      console.log('没有商品')
      this.setData({
        sumPrice:0,
        sumNum:0,
        cartList:myCart,
        listid:myCart
      })
    }
    console.log(myCart)
  },
  pay()
  {
    let message = wx.getStorageSync('message')
    console.log(message.length)
    if(message.length == 0)
    {
      wx.showToast({
        icon:'none',
        title: '您还没有填写地址',
        duration:2000,
        success:function()
        {
          setTimeout(function() 
          {
            wx.navigateTo({
              url: '/pages/address/address',
            })
          }, 2000);
          
        }
      })
    }
  }
})